<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {

    public function Post($curlPost,$url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
    }

    public function ajaxCheck()
    {
        if(IS_POST){
            $tel=I('post.shouji');
            $today=time();
            $c=date('Y-m-d H:i:s',$today);
            $b = substr($c,0,10);  //2017-04-24
            $model=M('Code');
            $data=$model->where(array('pnumber'=>$tel,
                'addtime'=>$b,
            ))->count();
            if($data <4){
                $ac='kiki1447';
                $pw='lhfyw123456';
                $code = $this->createSMSCode();
                $target = "http://sms.106jiekou.com/utf8/sms.aspx";
                //替换成自己的账号和接口密码
                $post_data = "account=".$ac."&password=".$pw."&mobile=".$tel."&content=".rawurlencode("您的订单编码：".$code."。如需帮助请联系客服。");
                echo $gets = $this->Post($post_data, $target);
                if($gets == 100){
                    session('code',$code,60);
                }
            }else{
                $this->ajaxReturn(array(
                    'info' => '1'
                ));
            }
        }
    }




    public function createSMSCode($length = 4){
        $min = pow(10 , ($length - 1));
        $max = pow(10, $length) - 1;
        return rand($min, $max);
    }


    public function index(){
       if($_POST){
        $admin=M("admin");
        $map['username'] = $_POST['username'];
        $user = $admin -> where($map) -> find();
        //echo $admin->getLastSql();die();
        if(empty($user)){
          die("<script>alert('用户名或者密码错误！');history.back(); </script>");
        }else{
            if($user['password'] == md5($_POST['password'])){
               $_SESSION['userid'] = $user['id'];
               $this->success('登录成功,正跳转至系统首页...', U('Index/dashboard'));
            }else{
                die("<script>alert('用户名或者密码错误！');history.back(); </script>");
            }            
        }

       }
        $this->display();
    }
    public function dashboard(){
        $this->checklogin();
        if(IS_POST){
            //dump($_POST);die;
            $model=D('Userinfo');
            if($model->create()){
            }else{
                $model->error($model->getError());
            }

        }else{
            $this->display();
        }

    }

    public function register()
    {
        if(IS_POST){
            $model=D('Userinfo');
            $db=M('Code');
            $tel=I('post.tel');
            $phonecode=I('post.phonecode');
            if(preg_match("/^1[34578]{1}\d{9}$/",$tel)){
                $v=session('code');
                $c=date('Y-m-d H:i:s',time());
                $b = substr($c,0,10);  //2017-04-24
                if($v == $phonecode){
                    //下面那东西，是不是需要放在create后？因为在自动验证失败后依然会保存进CODE表里
                    if($model->create()){
                        $data=array(
                            'pnumber'=>$tel,
                            'addtime'=>$b,
                            'code'=>$phonecode,
                        );
                        $db->data($data)->add();
                        if($model->add()){
                            $this->success('注册成功');
                        }else{
                            $this->success('注册失败');
                        }
                    }else{
                        $this->error($model->getError());
                    }
                }else{
                    $this->error('手机验证码不正确');
                }
            }else{
                $this->error('手机号码格式不正确');
            }
        }else{

            $this->display();
        }




    }

    public function checklogin(){
       $uid=$_SESSION['userid']; 
        if(!$uid)
        {
            $this->error('请登录','/index.php/Admin/Index/index');
        }
    }

}